﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain.Entities;
using WebNext.UI.Properties;

namespace WebNext.UI.Models.Entries
{
	public static class EntryElementFactory
	{
		public const string InputPattern = @"^\d+_\w+_\w+";

		public const char InputSplitter = '_';

		public static EntryElementViewModel CreateEntryElementViewModel(EntryElement entryElement)
		{
			switch (entryElement.Type)
			{
				case EntryElementType.Header:
					return new EntryHeaderElementViewModel(entryElement as EntryHeaderElement);
				case EntryElementType.Chapter:
					return new EntryChapterElementViewModel(entryElement as EntryChapterElement);
				case EntryElementType.Html:
					return new EntryHtmlElementViewModel(entryElement as EntryHtmlElement);
				case EntryElementType.Code:
					return new EntryCodeElementViewModel(entryElement as EntryCodeElement);
				case EntryElementType.Image:
					return new EntryImageElementViewModel(entryElement as EntryImageElement);
				default:
					throw new NotSupportedException(string.Format("Element of type {0} is not supported", entryElement.Type));
			}
		}

		public static EntryElementViewModel CreateEntryElementViewModel(int order, EntryElementType type)
		{
			return CreateEntryElementViewModel(order, type, new Dictionary<string, string>());
		}

		public static EntryElementViewModel CreateEntryElementViewModel(int order, EntryElementType type, IDictionary<string, string> properties)
		{
			switch (type)
			{
				case EntryElementType.Header:
					return new EntryHeaderElementViewModel(order, properties);
				case EntryElementType.Chapter:
					return new EntryChapterElementViewModel(order, properties);
				case EntryElementType.Html:
					return new EntryHtmlElementViewModel(order, properties);
				case EntryElementType.Code:
					return new EntryCodeElementViewModel(order, properties);
				case EntryElementType.Image:
					return new EntryImageElementViewModel(order, properties);
				default:
					throw new NotSupportedException(string.Format("Element of type {0} is not supported", type));
			}
		}

		public static IEnumerable<EntryElementTypeInfo> GetAvailableEntryElementTypesInfo()
		{
			yield return new EntryElementTypeInfo(EntryElementType.Header, Resources.AddHeaderName, Resources.AddHeaderTitle);
			yield return new EntryElementTypeInfo(EntryElementType.Chapter, Resources.AddChapterName, Resources.AddChapterTitle);
			yield return new EntryElementTypeInfo(EntryElementType.Html, Resources.AddHtmlName, Resources.AddHtmlTitle);
			yield return new EntryElementTypeInfo(EntryElementType.Code, Resources.AddCodeName, Resources.AddCodeTitle);
			yield return new EntryElementTypeInfo(EntryElementType.Image, Resources.AddImageName, Resources.AddImageTitle);
		}
	}
}